/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Vlada, Aleksandar
 */
public class ServerThread extends Thread {

    static int port = 4040;

    @Override
    public void run() {

        System.out.println("*** Nit pokrenuta");

        try {
            ServerSocket serverSocket = new ServerSocket(port);
            Robot robot = new Robot();

            int singleClick = 1;
            int doubleClick = 2;
            int dragAndDrop = 3;
            int keyboard = 4;

            Socket socket = serverSocket.accept();
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

            while (true) {

                int typeOfEvent = ois.readInt();
                if (typeOfEvent == singleClick) {
                    int x = ois.readInt();
                    int y = ois.readInt();
                    int button = ois.readInt();
                    robot.mouseMove(x, y);
                    if (button == 1) {
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                    } else if (button == 2) {
                        robot.mousePress(InputEvent.BUTTON2_MASK);
                        robot.mouseRelease(InputEvent.BUTTON2_MASK);
                    } else if (button == 3) {
                        robot.mousePress(InputEvent.BUTTON3_MASK);
                        robot.mouseRelease(InputEvent.BUTTON3_MASK);
                    }
                } else if (typeOfEvent == doubleClick) {
                    int x = ois.readInt();
                    int y = ois.readInt();
                    int button = ois.readInt();
                    robot.mouseMove(x, y);
                    if (button == 1) {
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                    } else if (button == 2) {
                        robot.mousePress(InputEvent.BUTTON2_MASK);
                        robot.mouseRelease(InputEvent.BUTTON2_MASK);
                        robot.mousePress(InputEvent.BUTTON2_MASK);
                        robot.mouseRelease(InputEvent.BUTTON2_MASK);
                    } else if (button == 3) {
                        robot.mousePress(InputEvent.BUTTON3_MASK);
                        robot.mouseRelease(InputEvent.BUTTON3_MASK);
                        robot.mousePress(InputEvent.BUTTON3_MASK);
                        robot.mouseRelease(InputEvent.BUTTON3_MASK);
                    }
                } else if (typeOfEvent == dragAndDrop) {
                    int startX = ois.readInt();
                    int startY = ois.readInt();
                    int endX = ois.readInt();
                    int endY = ois.readInt();
                    if (startX != endX && startY != endY) {
                        robot.mouseMove(startX, startY);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.delay(500);
                        robot.mouseMove(endX, endY);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                    }
                } else if (typeOfEvent == keyboard) {
                    int keyCode = ois.readInt();
                    boolean isShiftDown = ois.readBoolean();
                    boolean isControlDown = ois.readBoolean();
                    if (isShiftDown) {
                        robot.keyPress(16);
                    }
                    if (isControlDown) {
                        robot.keyPress(17);
                    }
                    robot.keyPress(keyCode);
                    robot.keyRelease(keyCode);
                    if (isShiftDown) {
                        robot.keyRelease(16);
                    }
                    if (isControlDown) {
                        robot.keyRelease(17);
                    }
                }
            }
        } catch (AWTException | IOException ex) {
            System.err.println(ex.getMessage());
        }
    }
}